//
//  STServiceListView.m
//  YiYueHairdresser
//
//  Created by zhouzhouzhou on 2018/9/30.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "STServiceListView.h"
#import "STServiceCell.h"

@implementation STServiceListView


-(id)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    tableFrame = frame;
    self.backgroundColor=[UIColor clearColor];
    return self;
}

-(void)loadServiceListView{
    if (!serviceListView) {
        serviceListView = [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
        serviceListView.delegate = self;
        serviceListView.dataSource = self;
        serviceListView.separatorStyle = UITableViewCellSelectionStyleNone;
        serviceListView.backgroundColor = [UIColor clearColor];
        serviceListView.sectionHeaderHeight = 10;
        WeakSelf
        serviceListView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakSelf downSlide];
        }];
        
        serviceListView.mj_footer = [MJRefreshBackNormalFooter  footerWithRefreshingBlock:^{
            [weakSelf upSlide];
        }];
        [self addSubview:serviceListView];
    }else{
        [serviceListView reloadData];
    }
    [serviceListView.mj_header endRefreshing];
    [serviceListView.mj_footer endRefreshing];
}


#pragma mark - tableView delegate


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.serviceListArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  200;
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * headerView = [UIView new];
    headerView.backgroundColor = [UIColor clearColor];
    return headerView;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString * identifier = @"STServiceCell";
    STServiceCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[STServiceCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    
    [cell.dowmBtn addTarget:self action:@selector(actionDownService:) forControlEvents:UIControlEventTouchUpInside];
    [cell.changeBtn addTarget:self action:@selector(actionChangeService:) forControlEvents:UIControlEventTouchUpInside];
    [cell setServiceCellType:self.serviceType];
    [cell setServiceCellModel:_serviceListArr[indexPath.section]];
    //    cell.indexL.text = [NSString stringWithFormat:@"%zd",indexPath.row];
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.serviceDelegate respondsToSelector:@selector(serviceListViewSelIndexPath:data:)]) {
        [self.serviceDelegate serviceListViewSelIndexPath:indexPath data:indexPath];
    }
}

-(void)actionDownService:(UIButton *)sender{
    UITableViewCell * cell = (UITableViewCell *)sender.superview.superview.superview;
    NSIndexPath * indexPath = [serviceListView indexPathForCell:cell];
    if ([self.serviceDelegate respondsToSelector:@selector(serviceListViewDownActionIndexPath:data:)]) {
        [self.serviceDelegate serviceListViewDownActionIndexPath:indexPath data:indexPath];
    }
}

-(void)actionChangeService:(UIButton *)sender{
    UITableViewCell * cell = (UITableViewCell *)sender.superview.superview.superview;
    NSIndexPath * indexPath = [serviceListView indexPathForCell:cell];
    if ([self.serviceDelegate respondsToSelector:@selector(serviceListViewChangeActionIndexPath:data:)]) {
        [self.serviceDelegate serviceListViewChangeActionIndexPath:indexPath data:indexPath];
    }
}

//向上滑动
-(void)upSlide{
    if ([self.serviceDelegate respondsToSelector:@selector(serviceListViewUpSlide)]) {
        [serviceListView.mj_footer beginRefreshing];
        [self.serviceDelegate serviceListViewUpSlide];
    }
}

//向下滑动
-(void)downSlide{
    if ([self.serviceDelegate respondsToSelector:@selector(serviceListViewDownSlide)]) {
        [serviceListView.mj_header beginRefreshing];
        [self.serviceDelegate serviceListViewDownSlide];
    }
}
@end
